package fr.pud.client.controller.query;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Queries;
import fr.pud.client.model.Query;
import fr.pud.client.view.QueryFrame;

/**
 * The controller to remove a query from queries (the singleton). Apr 6, 2012 -
 * tsp-osmBis
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         DeleteQueryFromQueries.java
 */
public class DeleteQueryFromQueries extends AbstractAction {
    /**
     * Serial UID
     */
    private static final long serialVersionUID = 5020265799661101736L;

    /**
     * Default constructor
     */
    public DeleteQueryFromQueries() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.DELETE_QUERY));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.DELETE_QUERY));
        this.putValue(SMALL_ICON, ImagesDatabase.FULL_TRASH);
    }

    /**
     * Delete the selected query.
     */
    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        Queries.getInstance().removeQuery(
                (Query) QueryFrame.getInstance().getListQueries()
                        .getSelectedValue());
        if (Queries.getInstance().getQueries().size() == 0) {
            QueryFrame.getInstance().getButtonModify().setVisible(false);
            QueryFrame.getInstance().getButtonDelete().setVisible(false);
        }
        else {
            QueryFrame.getInstance().getButtonModify().setVisible(true);
            QueryFrame.getInstance().getButtonDelete().setVisible(true);
        }
    }
}
